package world.pieces;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import javax.imageio.ImageIO;

import utility.Color;
import utility.Path;
import utility.Point;
import world.Piece;


public class King extends Piece implements Cloneable, Serializable{
	boolean debug=false;
	Point firstKingWhitePosition= new Point(7, 4);
	Point firstKingBlackPosition= new Point(0, 4);
	public King(Color c) {
		this.color=c;
	}
	
	public boolean canMove(Point from, Point to) {
		if(this.color.equals(Color.WHITE)){
			if(from.equals(firstKingWhitePosition)){
				if(from.getX()==to.getX() && from.getY()==to.getY()+2){
					return true;
				}
				if(from.getX()==to.getX() && from.getY()==to.getY()-2){
					return true;
				}
			}
		}else if (this.color.equals(Color.BLACK)){
			if(from.equals(firstKingBlackPosition)){
				if(from.getX()==to.getX() && from.getY()==to.getY()+2){
					return true;
				}
				if(from.getX()==to.getX() && from.getY()==to.getY()-2){
					return true;
				}
			}
		}

		
		if(from.getX()==to.getX() && from.getY()==to.getY()+1){
			return true;
		}
		if(from.getX()==to.getX() && from.getY()==to.getY()-1){
			return true;
		}
		if(from.getX()==to.getX()+1 && from.getY()==to.getY()){
			return true;
		}
		if(from.getX()==to.getX()-1 && from.getY()==to.getY()){
			return true;
		}
		
		
		
		if(from.getX()==to.getX()+1 && from.getY()==to.getY()+1){
			return true;
		}
		if(from.getX()==to.getX()-1 && from.getY()==to.getY()-1){
			return true;
		}
		
		if(from.getX()==to.getX()+1 && from.getY()==to.getY()-1){
			return true;
		}
		if(from.getX()==to.getX()-1 && from.getY()==to.getY()+1){
			return true;
		}
		

		if(debug){
			System.out.println("King can't move from " +from +" to "+to);
		}
		return false;
		// TODO Auto-generated method stub

	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "(King "+color.getColor()+")";
	}
	public BufferedImage get2dImage() {
		// TODO Auto-generated method stub
		if(this.color.equals(Color.WHITE)){
			try {
				return  ImageIO.read(new File(Path.getPathPiecesWhite()+"king-white.png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(this.color.equals(Color.BLACK)){
			try {
				return  ImageIO.read(new File(Path.getPathPiecesBlack()+"king-black.png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	@Override
	public Object clone() {
		// TODO Auto-generated method stub
		return super.clone();
	}
	@Override
	public String get2DPath() {
		if(this.color.equals(Color.WHITE)){

				return  Path.getPathPiecesWhite()+"king-white.png";
	
		}
		else if(this.color.equals(Color.BLACK)){
			
				return  Path.getPathPiecesBlack()+"king-black.png";

		}else{
			return "";
		}
	}
}
